package com.mo.axion.cron.handler;

import com.dtp.core.thread.DtpExecutor;
import com.mo.axion.cron.config.CronAsyncThreadPoolConfig;
import com.mo.axion.cron.service.TaskHandler;
import com.mo.axion.support.utils.ThreadPoolUtils;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Mo
 * @createTime 2024/4/3 17:10
 * @description 定时任务处理器
 */
@Slf4j
@Service
public class CronTaskHandler {
    @Autowired
    private TaskHandler taskHandler;

    @Autowired
    private ThreadPoolUtils threadPoolUtils;
    private DtpExecutor dtpExecutor = CronAsyncThreadPoolConfig.getXxlCronExecutor();

    @XxlJob("axionJob")
    public void execute() {
        log.info("CronTaskHandler#execute messageTemplateId:{} cron exec!", XxlJobHelper.getJobParam());
        threadPoolUtils.register(dtpExecutor);
        Long messageTemplateId = Long.valueOf(XxlJobHelper.getJobParam());
        dtpExecutor.execute(() -> taskHandler.handle(messageTemplateId));
    }
}
